#ifndef JSON_H
#define JSON_H
#include "Lexer.h"
#include "List.h"


typedef enum
{
	JSON_VALUE_TYPE_STRING,
	JSON_VALUE_TYPE_NUMBER,
	JSON_VALUE_TYPE_BOOLEAN,
	JSON_VALUE_TYPE_NULL,
	JSON_VALUE_TYPE_OBJECT,
	JSON_VALUE_TYPE_ARRAY,
}JSON_VALUE_TYPE;

typedef struct _Json_Object
{
	list_t values; //values
}Json_Object;

typedef struct
{
	JSON_VALUE_TYPE type;
	String_t name;
	union
	{
		String_t _string;
		double _number;
		unsigned char   _boolean;
		Json_Object _object;
		list_t _array;//values
	};
}Json_Value;

typedef struct _Json_Object Json_Object;

typedef struct  
{
	memorypool *mp;
	Json_Value rootValue;
}Json;


Json_Value *JsonGetObjectValue(Json_Value *json_value,const char name[]);
Json_Value *JsonGetObjectValueByIndex(Json_Value *json_value,int i);
Json_Value* JsonValueGetValue(Json_Value* it, const char _payload[]);
Json_Value* JsonGetValue(Json* json, const char payload[]);
Json_Value * JsonGetArrayValue(Json_Value *value,int i);


unsigned char JsonInitialize(memorypool *mp,Json *pjson);

unsigned char JsonParse(Json *pjson,const char *json_content);
unsigned char JsonBuild_Value(Json_Value *pValue,String_t *_out,unsigned char bArrayValue);
unsigned char JsonBuild(Json *pjson,String_t *_out);
void JsonDelete(Json *pjson,const char payload[]);
void JsonFree(Json *pjson);
void JsonClear(Json* pjson);

unsigned char JsonCreateObjectValue(memorypool* mp, Json_Value* pValue, const char name[]);
unsigned char JsonCreateStringValue(memorypool *mp,Json_Value *pValue,const char name[],const char text[]);
unsigned char JsonCreateNumberValue(memorypool *mp,Json_Value *pValue,const char name[],double value);
unsigned char JsonCreateBooleanValue(memorypool *mp,Json_Value *pValue,const char name[],unsigned char b);
unsigned char JsonCreateArrayValue(memorypool *mp,Json_Value *pValue,const char name[]);

unsigned char JsonAddString(Json *pjson,const char parent_payload[],const char name[],const char text[]);
unsigned char JsonAddNumber(Json *pjson,const char parent_payload[],const char name[],const double number);
unsigned char JsonAddBoolean(Json *pjson,const char parent_payload[],const char name[],const unsigned char b);
unsigned char JsonAddArray(Json *pjson,const char parent_payload[],const char name[]);
unsigned char JsonAddObject(Json *pjson,const char parent_payload[],const char name[]);
//px_color JsonGetColor(Json* pjson, const char payload[]);

const char *JsonGetString(Json *pjson,const char payload[]);
double JsonGetNumber(Json *pjson,const char payload[]);
unsigned char  JsonGetBoolean(Json *pjson,const char payload[]);

const char* JsonValueGetString(Json_Value* pValue, const char payload[]);
double JsonValueGetNumber(Json_Value* pValue, const char payload[]);
unsigned char  JsonValueGetBoolean(Json_Value* pValue, const char payload[]);

unsigned char JsonSetString(Json *pjson,const char payload[],const char text[]);
unsigned char JsonSetNumber(Json *pjson,const char payload[],const double number);
unsigned char JsonSetBoolean(Json *pjson,const char payload[],const unsigned char b);

unsigned char JsonObjectAddValue(Json_Value *pObject,Json_Value *value);
unsigned char JsonArrayAddValue(Json_Value *pArray,Json_Value *value);

#endif

